<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/8
 * Time: 0:19
 */

namespace common\models\Service;


use common\models\DAO\UserIntegralList;
use common\models\DAO\UserIntegralLog;
use common\models\DAO\UserInvestList;

class UserIntegral {
    public static function get($userID){
        $integral = \common\models\DAO\UserIntegral::find()->where(['userID'=>$userID])->andFilterWhere(['>', 'usable', 0])->groupBy('userID')
            ->select('SUM(usable) as usable')
            ->one();
        if($integral){
            return intval($integral['usable']);
        }else
            return 0;
    }

    /*
     * 积分扣除
     */
    public static function deduct($param){

        $userID = $param['userID'];
        $integral = $param['integral'];
        $now = $param['createTime'];
        $integral_model = \common\models\DAO\UserIntegral::find()->where(['userID'=>$userID])->andFilterWhere(['>', 'usable', 0])
            ->orderBy('createTime asc')
            ->all();
        $couponID = $param['couponID'];
        $hongBaoID = $param['hongBaoID'];
        $lotteryID = $param['lotteryID'];
        $desc = $param['desc'];
        $road = $param['road'];
        $user_integral_log = new UserIntegralLog();
        $user_integral_log->userID = $userID;
        $user_integral_log->createTime = $now;
        $user_integral_log->integral = $integral;
        $user_integral_log->type = 2;
        $user_integral_log->road = $road;
        $user_integral_log->des = $desc;
        $user_integral_log->couponID = $couponID;
        $user_integral_log->hongBaoID = $hongBaoID;
        $user_integral_log->lotteryID = $lotteryID;
        $user_integral_log_result = $user_integral_log->save();
        if(!$user_integral_log_result)
            return false;
        //支出日志


        foreach($integral_model as $v){
            $cur_integral = intval($v['usable']);
            $id = $v['id'];
            if($cur_integral <= 0)
                continue;
            if($cur_integral < $integral){
                $integral_one = \common\models\DAO\UserIntegral::findOne($id);
                $integral_one->usable = 0;
                $integral_one->updateTime = $now;
                $one_result = $integral_one->save();
                if(!$one_result)
                    return false;
                $IntegralList = new UserIntegralList();
                $IntegralList->userID = $userID;
                $IntegralList->integral = $cur_integral;
                $IntegralList->createTime = $now;
                $IntegralList->integralID = $id;
                $IntegralList->couponID = $couponID;
                $IntegralList->hongBaoID = $hongBaoID;
                $IntegralList->lotteryID = $lotteryID;
                $list_result = $IntegralList->save();
                if(!$list_result)
                    return false;
                $integral = $integral - $cur_integral;
            }else{
                $integral_one = \common\models\DAO\UserIntegral::findOne($id);
                $integral_one->usable = $cur_integral - $integral;
                $integral_one->updateTime = $now;
                $one_result = $integral_one->save();
                if(!$one_result)
                    return false;
                $IntegralList = new UserIntegralList();
                $IntegralList->userID = $userID;
                $IntegralList->integral = $integral;
                $IntegralList->createTime = $now;
                $IntegralList->integralID = $id;
                $IntegralList->couponID = $couponID;
                $IntegralList->hongBaoID = $hongBaoID;
                $IntegralList->lotteryID = $lotteryID;
                $list_result = $IntegralList->save();
                if(!$list_result)
                    return false;
                break;
            }
        }
        return true;
    }
}